<?php

/**
 * 
 * @project     : innoTweets 1.0
 * @author      : Dino (DBK)
 * 
 * @package     : Notification Mobile
 * @description : 
 * 
 */
if (!defined('BASEPATH')) {
  exit('No direct script access allowed');
}

class Notification extends MOBILE_AUTH_Controller {

  public function counts() {

    // Init vars
    $response = array();
    $response['header'] = $this->header_counts();
    $response['unread'] = $this->notify_list_count();

    $this->response['status'] = 1;
    $this->response['message'] = 'Success';
    $this->response['data'] = $response;
  }

  public function items() {
    $this->response['status'] = 1;
    $this->response['message'] = 'Success';
    $this->response['data'] = $this->notify_list();
  }

  /**
   * Counts for the header menu
   * 
   */
  private function header_counts() {

    // Getting header counts
    $header_counts = $this->settings_model->get_header_counts();

    // Preparing response array
    $response = array();
    $response['tweets'] = (int) $header_counts['result']['tweet_total_count'];
    $response['events'] = (int) $header_counts['result']['event_total_count'] + $header_counts['result']['birthday_total_count'] + $header_counts['result']['joinday_total_count'];
    $response['albums'] = (int) $header_counts['result']['album_total_count'];
    $response['polls'] = (int) $header_counts['result']['poll_total_count'];
    $response['groups'] = (int) 0;
    $response['online'] = (int) $header_counts['result']['online_total_count'];

    // Responding json
    return $response;
  }

  /**
   * Preparing toast notificaion list unread count
   * 
   */
  private function notify_list_count() {

    $notify = array();

    // Getting notifications from cache
    $user_notifications = $this->cache->get('user_notify_' . $this->mobile_current_user['user_id']);

    // Preparing notifications
    if (!empty($user_notifications) && is_array($user_notifications)) {
      foreach ($user_notifications as $notification) {
        if (!empty($notification)) {
          if (($notification['read'] == 0)) {
            $notify[] = (string) $notification['text'];
          }
        }
      }
    }

    return (int) count($notify);
  }

  /**
   * Preparing toast notificaion list data
   * 
   */
  private function notify_list() {

    $notify = array();

    // Getting notifications from cache
    $user_notifications = $this->cache->get('user_notify_' . $this->mobile_current_user['user_id']);

    // Preparing notifications (Show recent 10 items)
    $count = 0;
    if (!empty($user_notifications) && is_array($user_notifications)) {
      @usort($user_notifications, array('notification', 'date_compare'));
      foreach ($user_notifications as $notification) {
        if (!empty($notification) && $count < 11) {
          $notify[] = array('text' => $notification['text'], 'post' => $notification['post_id']);
        }
        $count++;
      }
    }

    // Update toast viewed status
    $this->cache->delete('user_notify_' . $this->mobile_current_user['user_id']);
    $this->cache->save('user_notify_' . $this->mobile_current_user['user_id'], array_update($user_notifications, 'read', 1), 32400);

    return $notify;
  }

  private function date_compare($a, $b) {
    $t1 = strtotime(date('Y-m-d H:i:s', strtotime($a['date'])));
    $t2 = strtotime(date('Y-m-d H:i:s', strtotime($b['date'])));
    return $t2 - $t1;
  }

  public function __destruct() {
    echo json_encode($this->response);
  }

}

?>